package com.paomi.client.activity.newsDetail;

import android.os.Build;
import android.support.annotation.NonNull;
import android.view.MotionEvent;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.TextView;

import com.paomi.client.R;
import com.paomi.client.activity.newsDetail.model.NewsBean;
import com.paomi.client.activity.newsDetail.model.NewsListJson;
import com.paomi.client.base.BaseMvpActivity;

import butterknife.BindView;

public class NewsDetailActivity extends BaseMvpActivity<NewsMvpView, NewsDetailPresenter> implements NewsMvpView {

    @BindView(R.id.news_title)
    TextView newsTitle;
    @BindView(R.id.new_name)
    TextView newName;
    @BindView(R.id.new_time)
    TextView newTime;
    @BindView(R.id.news_web)
    WebView newsWeb;
    private NewsListJson newsListJson;
    private String getId;

    @NonNull
    @Override
    public NewsDetailPresenter createPresenter() {
        return new NewsDetailPresenter();
    }

    @Override
    protected String setTitle() {
        return getResources().getString(R.string.detail_txt);
    }

    @Override
    protected void showBack(TextView button_goback) {
        button_goback.setVisibility(View.VISIBLE);
        button_goback.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0);
        button_goback.setTextColor(getResources().getColor(R.color.main_color));
        button_goback.setText(getResources().getString(R.string.back_normal) + "返回");
        button_goback.setTypeface(iconfont);
    }

    @NonNull
    @Override
    protected int getLayoutId() {
        return R.layout.activity_news_detail;
    }

    @Override
    protected void initEvent() {
        getId = getIntent().getStringExtra("newId");
        newsWeb.getSettings().setJavaScriptEnabled(true);
        newsWeb.getSettings().setBlockNetworkImage(false);//解决图片不显示
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            newsWeb.getSettings().setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
        }
        getPresenter().setDetailNew("" + getId);
        newsWeb.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                return true;
            }
        });
    }

    @Override
    public void setDetailData(NewsBean newsBean) {
        newsListJson = newsBean.getReturnData();
        if(newsListJson!=null){
            newsTitle.setText(newsListJson.getTitle());
            newName.setText(newsListJson.getSource());
//        newTime.setText(newsListJson.getSource());
            String htmlData = newsListJson.getContent().replace("<img", "<img style='max-width:90%;height:auto;'");
            newsWeb.loadData(htmlData, "text/html; charset=UTF-8", null);
        }
    }

}
